//
//  AppDelegate.h
//  呼吸训练
//
//  Created by 1234 on 2024/5/8.
//

#import "AppDelegate.h"

#import "AppDelegate+Notification.h"

#import "LGTabBarViewController.h"
#import "LGNavigationController.h"
#import "LGLoginViewController.h"
#import <ImSDK_Plus/ImSDK_Plus.h>
#import "TUILogin.h"
#import <Bugly/Bugly.h>
#import "ZFLandscapeRotationManager.h"

//测试
#import "LGBodayStatisticsController.h"
#import "LGPatientDetailController.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self registNotification:application andOptions:launchOptions];
    [Bugly startWithAppId:@"6676d1c63d"];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    //统一设置
    [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    [UITableView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    if (@available(iOS 15.0,*)) {
        [UITableView appearance].sectionHeaderTopPadding = 0;
    }
    
//    [UIApplication sharedApplication].keyWindow.rootViewController = [LGLaunchViewController new];
//    LGNavigationController *navController = [[LGNavigationController alloc]initWithRootViewController:[[LGLoginTestController alloc] init]];//LGLoginTestController 、LGBBindingRolesController
//    self.window.rootViewController = navController;//navController;//[ViewController new];
    
    [self checkAnonymous];
    
    if (kIsLogin && [kIsLogin isEqualToString:@"Y"]) {
        //已登录
        [LGLoginManager loginTencentCloud];//登录腾讯云
        LGTabBarViewController *controller = [[LGTabBarViewController alloc]init];
        self.window.rootViewController = controller;
        
        //测试
//        [self checkTestController];

    }else{
        LGNavigationController *navController = [[LGNavigationController alloc]initWithRootViewController:[[LGLoginViewController alloc] init]];
        self.window.rootViewController = navController;
    }
    //显示启动页的时间
    [NSThread sleepForTimeInterval:1];
    return YES;
}

-(void)checkAnonymous{
    NSDate *today = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    // 时间格式,此处遇到过坑,建议时间HH大写,手机24小时进制和12小时禁止都可以完美格式化
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSDate *start = [dateFormat dateFromString:@"2024-04-19 09:30"];
    NSDate *expire = [dateFormat dateFromString:@"2024-04-20 10:00"];
    
    if ([today compare:start] == NSOrderedDescending && [today compare:expire] == NSOrderedAscending) {
        [[NSUserDefaults standardUserDefaults] setObject:@"Y" forKey:@"isFree"];
    }else{
        [[NSUserDefaults standardUserDefaults] setObject:@"N" forKey:@"isFree"];
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/// 在这里写支持的旋转方向，为了防止横屏方向，应用启动时候界面变为横屏模式
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    ZFInterfaceOrientationMask orientationMask = [ZFLandscapeRotationManager supportedInterfaceOrientationsForWindow:window];
    if (orientationMask != ZFInterfaceOrientationMaskUnknow) {
        return (UIInterfaceOrientationMask)orientationMask;
    }
    /// 这里是非播放器VC支持的方向
    return UIInterfaceOrientationMaskPortrait;
}


//测试跳转
-(void)checkTestController{
    //患者详情
    LGPatientDetailController *controller = [[LGPatientDetailController alloc]init];
    controller.patientid = @"1813770115513675777";
    
    //体征数据
//    LGBodayStatisticsController *controller = [[LGBodayStatisticsController alloc]init];
//    controller.patientid = @"1813770115513675777";
    //=====
    LGNavigationController *navController = [[LGNavigationController alloc]initWithRootViewController:controller];
    self.window.rootViewController = navController;
}


#pragma mark
#pragma mark ====> APP生命周期
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
//    [self.fwDelegate applicationWillResignActive:application];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    
//    @"HomeAfterDoctorViewController"
    
//    UIViewController *vc = [self getCurrentVC];
//    NSLog(@"%@",vc);
//    [self.fwDelegate applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    
//    UIViewController *vc = [self getCurrentVC];
//    NSLog(@"%@",vc);
//
//    [self.fwDelegate applicationWillEnterForeground:application];
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        UIViewController *vc = [self getCurrentVC];
//        NSLog(@"%@",[NSThread currentThread]);
//        if ([vc isKindOfClass:[PatientDetailViewController class]] && ((PatientDetailViewController *)vc).patientType == conversationPatientType) {
//            [[RCIMClient sharedRCIMClient] clearMessagesUnreadStatus:ConversationType_PRIVATE targetId:((PatientDetailViewController *)vc).tokenid];
//            [UIApplication sharedApplication].applicationIconBadgeNumber = [[RCIMClient sharedRCIMClient] getUnreadCount:@[@(ConversationType_PRIVATE)]];
//        }
//    });
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
//    [self.fwDelegate applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
//    [self.fwDelegate applicationWillTerminate:application];
}



@end

